package org.trinity.netdisk.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.trinity.netdisk.entity.User;
import org.trinity.netdisk.enums.ResultCode;
import org.trinity.netdisk.exception.user.ExistEmailException;
import org.trinity.netdisk.exception.user.InvalidParamterException;
import org.trinity.netdisk.exception.user.InvalidVerificationCodeException;
import org.trinity.netdisk.exception.user.NotExistEmailException;
import org.trinity.netdisk.service.IUserService;
import org.trinity.netdisk.util.JWTUtil;
import org.trinity.netdisk.util.ResponseWrapper;

@SuppressWarnings("unused")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @Autowired
    StringRedisTemplate stringRedisTemplate;


    @RequestMapping("/login")
    private Object login(@RequestBody User user) {
        String token = userService.login(user);
        if (token == null) {
            return new ResponseWrapper(ResultCode.USER_LOGIN_INCORRECT_USERNAME_OR_PASSWD);
        }
        return JWTUtil.getUserVale(token).getStatus()
                ? new ResponseWrapper(ResultCode.SUCCESS, token)
                : new ResponseWrapper(ResultCode.USER_ACCOUNT_FORBIDDEN);
    }

    @RequestMapping("/sendVerificationCode/{email}")
    private Object sendVerificationCode(@PathVariable("email") String email) {
        try {
            userService.sendVerificationCode(email);
            return new ResponseWrapper(ResultCode.SUCCESS);
        } catch (ExistEmailException e) {
            return new ResponseWrapper(ResultCode.EXIST_EMAIL);
        } catch (Exception e) {
            e.printStackTrace();
            stringRedisTemplate.delete(email);
            return new ResponseWrapper(ResultCode.SYSTEM_INNER_ERROR);
        }
    }

    @RequestMapping("/register/{verCode}")
    private Object register(@RequestBody User user, @PathVariable("verCode") String verCode) {
        try {
            userService.register(user, verCode);
            return new ResponseWrapper(ResultCode.SUCCESS);
        } finally {
            stringRedisTemplate.delete(user.getEmail());
        }
    }

    @RequestMapping("/isExistUsername/{username}")
    private Object isExistUsername(@PathVariable String username) {
        return new ResponseWrapper(
                userService.isExistUsername(username) ?
                        ResultCode.USERNAME_HAS_EXISTED :
                        ResultCode.SUCCESS
        );
    }

    @RequestMapping("/isExistEmail/{email}")
    private Object isExistEmail(@PathVariable("email") String email) {
        return new ResponseWrapper(
                userService.isExistEmail(email) ?
                        ResultCode.EXIST_EMAIL :
                        ResultCode.SUCCESS
        );
    }
    
    @RequestMapping("/info/{token}")
    private Object info(@PathVariable String token) {
        if (JWTUtil.verify(token)) {
            return new ResponseWrapper(ResultCode.SUCCESS, JWTUtil.getUserVale(token));
        }
        return new ResponseWrapper(ResultCode.PARAM_IS_INVALID);
    }

    @RequestMapping("/sendResetPasswdVerificationCode/{email}")
    private Object sendResetPasswdVerificationCode(@PathVariable("email") String email){
        try {
            userService.sendResetPasswdVerificationCode(email);
            return new ResponseWrapper(ResultCode.SUCCESS);
        }catch (NotExistEmailException e){
            return new ResponseWrapper(ResultCode.NOT_EXIST_EMAIL);
        }
    }

    @RequestMapping("/resetPassword")
    private Object resetPassword(String email,String password,String verCode){
        userService.resetPasswd(email, password, verCode);
        return new ResponseWrapper(ResultCode.SUCCESS);
    }
}
